package com.kan.awaits.mongo;

import java.io.Serializable;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;

/**
 * 用来操作整个数据库
 * 
 * @author john
 * @date 2011-4-12
 */
public interface Datastore
{
	/**
	 * save this entity and return his id
	 * 
	 * @param entity
	 * @return
	 * @throws DatastoreException
	 */
	public Serializable save(Object entity) throws DatastoreException;

	/**
	 * delete an entity by it's id
	 * 
	 * @param entity
	 * @return
	 * @throws DatastoreException
	 */
	public boolean delete(Object entity) throws DatastoreException;

	/**
	 * @param id
	 * @return
	 * @throws DatastoreException
	 */
	public boolean delete(Class<?> type, Serializable id) throws DatastoreException;

	/**
	 * get an object by it's id
	 * 
	 * @param <T>
	 * @param type
	 * @param id
	 * @return
	 * @throws DatastoreException
	 */
	public <T> T get(Class<T> type, Serializable id);

	/**
	 * Does a query to check if the id exists in mongodb
	 * 
	 * @param type
	 * @param id
	 * @return
	 * @throws DatastoreException
	 */
	boolean exists(Class<?> type, Serializable id) throws DatastoreException;

	/**
	 * update an entity,if the entity is not exist in db ,it will throws an Exception
	 * 
	 * @param entity
	 * @throws DatastoreException
	 */
	public void update(Object entity) throws DatastoreException;

	/**
	 * ensure capped DBCollections for {@code Entity}(s)
	 */
	public void ensureCaps();

	/**
	 * get the Mongo's DB
	 * 
	 * @return
	 */
	public DB getDB();

	/**
	 * get mongo
	 * 
	 * @return
	 */
	public Mongo getMongo();

	/**
	 * 
	 * @param entityType
	 * @return
	 */
	public DBCollection getCollection(Class<?> entityType);

	/**
	 * @param collectionName
	 * @return
	 */
	public DBCollection getCollection(String collectionName);

	/**
	 * 
	 * @return
	 */
	public WriteConcern getDefaultWriteConcern();

	/**
	 * 
	 * @param wc
	 */
	public void setDefaultWriteConcern(WriteConcern wc);
}
